﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using XcoAppSpaces.Core;
using System.Diagnostics;
using System.Threading;

namespace AlternativePorts.Tests
{
	[TestFixture]
	public class PortExtensionsTests
	{
		Port<int> port;

		[SetUp]
		public void SetUp()
		{
			port = new Port<int>();
		}

		[Test]
		public void TestReceiveSync_ItemAvailable()
		{
			port.Post(123);
			var item = port.ReceiveSync();

			Assert.AreEqual(123, item);
		}

		[Test]
		[ExpectedException(typeof(TimeoutException))]
		public void TestReceiveSync_NoItemAvailable()
		{
			port.ReceiveSync(1000);
		}

		[Test]
		[ExpectedException(typeof(TimeoutException))]
		public void TestReceiveSync_OneItemAvailable_SecondReceiveThrowsEx()
		{
			port.Post(123);
			var item = port.ReceiveSync();

			Assert.AreEqual(123, item);

			port.ReceiveSync(1000);
		}

		[Test]
		public void TestReceiveSync_TwoItemsAvailable_OnlyOneTaken()
		{
			port.Post(123);
			port.Post(456);
			var item = port.ReceiveSync();

			Assert.AreEqual(123, item);
			Assert.AreEqual(1, port.ItemCount);
		}
	}
}
